import Vue from 'vue';

export const platForms = {
    ua: window.navigator.userAgent,
    is: function (type) {
        type = type.toLowerCase();
        return this.ua.toLowerCase().indexOf(type) >= 0;
    },
    isMobile: function () {
        return !!this.ua.match(/(iPhone|iPod|Android|ios)/i);
    },
    isIOS: function () {
        return !!this.ua.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    },
    isIPad: function () {
        return this.is('ipad');
    },
    isiPhone: function () {
        return this.is('iPhone');
    },
    isAndroid: function () {
        return this.is('android') || this.is('Linux');
    },
    isWindowsPhone: function () {
        return this.is('Windows Phone');
    },
    isWx: function () {
        return this.is('micromessenger');
    }
}

// 监听网络状态
export function checkNetWorkStatus() {
    document.addEventListener('offline', function () {
        Vue.prototype.$status = 'off'
    }, false);
    document.addEventListener('online', function () {
        if (navigator.connection.type == "WIFI") {
            Vue.prototype.$status = 'WIFI'
        } else {
            Vue.prototype.$status = '4G'
        }
    }, false);
}

// 设置本地存储
export function setData(key, value) {
    window.localStorage.setItem(key, value)
}

// 获取本地存储
export function getData(key) {
    var data = window.localStorage.getItem(key) ? window.localStorage.getItem(key) : ''
    return data
}

// 删除本地存储
export function removeData(key) {
    window.localStorage.removeItem(key)
}

// 删除所有本地存储
export function clearAllData() {
    window.localStorage.clear()
}

